package com.ezcoding.dao;

import com.ezcoding.model.HistoryProduct;
import com.ezcoding.model.Product;
import com.jfinal.plugin.activerecord.Db;

import java.util.List;

public class ProductDao {
    public static ProductDao dao = new ProductDao();

    public Product getProductByCompanyIdAndProductId(String companyId, String productId) {
        String sql = Db.getSql("Product.getProductByCompanyIdAndProductId");

        return Product.dao.findFirst(sql, companyId, productId);
    }

    public List<Product> getProductsByCompanyId(String companyId) {
        String sql = Db.getSql("Product.getProductsByCompanyId");

        return Product.dao.find(sql, companyId);
    }

    public Long getProductsCountByCompanyIdAndPrimaryDirectory(String companyId, String primaryDirectory) {
        String sql = Db.getSql("Product.getProductsCountByCompanyIdAndPrimaryDirectory");

        return Db.queryLong(sql, companyId, primaryDirectory);
    }

    public Long getProductsCountByCompanyIdAndDirectory(String companyId, String primaryDirectory, String secondaryDirectory) {
        String sql = Db.getSql("Product.getProductsCountByCompanyIdAndDirectory");

        return Db.queryLong(sql, companyId, primaryDirectory, secondaryDirectory);
    }

    public boolean updateProductsPrimaryDirectory(String companyId, String oldPrimaryDirectory, String newPrimaryDirectory) {
        String sql = Db.getSql("Product.getProductsByCompanyIdAndPrimaryDirectory");

        List<Product> products = Product.dao.find(sql, companyId, oldPrimaryDirectory);

        for (Product product : products) {
            product.setPrimaryDirectory(newPrimaryDirectory);
            if (!product.update()) return false;
        }

        return true;
    }

    public boolean updateProductsDirectory(
            String companyId,
            String oldPrimaryDirectory,
            String oldSecondaryDirectory,
            String newSecondaryDirectory) {
        String sql = Db.getSql("Product.getProductsByCompanyIdAndDirectory");

        List<Product> products = Product.dao.find(sql, companyId, oldPrimaryDirectory, oldSecondaryDirectory);

        for (Product product : products) {
            product.setSecondaryDirectory(newSecondaryDirectory);
            if (!product.update()) return false;
        }

        return true;
    }

    public HistoryProduct getHistoryProduct(
            String sourceCompanyId,
            String targetCompanyId,
            String sourceProductId,
            String targetProductId,
            int type,
            int queryType) {
        HistoryProduct historyProduct;
        if (queryType == 0) {
            historyProduct = getHistoryProductBySourceProductId(
                    sourceCompanyId,
                    targetCompanyId,
                    sourceProductId,
                    type);
            if (historyProduct == null) {
                historyProduct = getHistoryProductByTargetProductId(
                        sourceCompanyId,
                        targetCompanyId,
                        targetProductId,
                        type);
            }
        } else {
            historyProduct = getHistoryProductByTargetProductId(
                    sourceCompanyId,
                    targetCompanyId,
                    targetProductId,
                    type);

            if (historyProduct == null) {
                historyProduct = getHistoryProductBySourceProductId(
                        sourceCompanyId,
                        targetCompanyId,
                        sourceProductId,
                        type);
            }
        }

        return historyProduct;
    }

    public HistoryProduct getHistoryProductBySourceProductId(
            String sourceCompanyId,
            String targetCompanyId,
            String sourceProductId,
            int type) {
        String sql = Db.getSql("Product.getHistoryProductBySourceProductId");

        return HistoryProduct.dao.findFirst(sql, sourceCompanyId, targetCompanyId, sourceProductId, type);
    }

    public HistoryProduct getHistoryProductByTargetProductId(
            String sourceCompanyId,
            String targetCompanyId,
            String targetProductId,
            int type) {
        String sql = Db.getSql("Product.getHistoryProductByTargetProductId");

        return HistoryProduct.dao.findFirst(sql, sourceCompanyId, targetCompanyId, targetProductId, type);
    }

    public List<HistoryProduct> getHistoryProducts(
            String sourceCompanyId,
            String targetCompanyId,
            String sourceProductId,
            int type) {
        String sql = Db.getSql("Product.getHistoryProducts");

        return HistoryProduct.dao.find(sql, sourceCompanyId, "%" + targetCompanyId + "%", "%" + sourceProductId + "%", type);
    }
}
